package pers.cz.common.sharding;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: Jef-orm
 * @description: 强制指定sharding分表
 * @author: Cheng Zhi
 * @create: 2023-11-01 14:45
 **/
public class ForceShardingRule implements ShardingRule {

    private List<Ruling> rulings;

    @Override
    public void clearShardingRule() {
        rulings.clear();
    }

    @Override
    public void addShardingRule(Object value, ShardingStrategy shardingStrategy, String modulus, String appender) {
        if (rulings == null) {
            rulings = new ArrayList<Ruling>();
        }

        Ruling ruling = new Ruling();
        ruling.setValue(value);
        ruling.setShardingStrategy(shardingStrategy);
        ruling.setModulus(modulus);
        ruling.setAppender(appender);
        rulings.add(ruling);
    }

    @Override
    public void addShardingRule(Object value, ShardingStrategy shardingStrategy) {

        if (rulings == null) {
            rulings = new ArrayList<Ruling>();
        }

        Ruling ruling = new Ruling();
        ruling.setValue(value);
        ruling.setShardingStrategy(shardingStrategy);
        rulings.add(ruling);
    }

    @Override
    public void addShardingRule(Object value, ShardingStrategy shardingStrategy, String modulus) {

        if (rulings == null) {
            rulings = new ArrayList<Ruling>();
        }

        Ruling ruling = new Ruling();
        ruling.setValue(value);
        ruling.setShardingStrategy(shardingStrategy);
        ruling.setModulus(modulus);
        rulings.add(ruling);
    }

    @Override
    public List<Ruling> getShardingRule() {
        return rulings;
    }
}
